<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="园区编码" prop="parkId">
        <el-select clearable v-model="queryParams.parkNum" placeholder="请选择园区编码">
          <el-option
            v-for="item in parkList"
            :key="item.id"
            :label="item.parkName"
            :value="item.parkNum"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="变压器" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入变压器名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!--<el-form-item label="负荷" prop="curLoad">-->
        <!--<el-input-->
          <!--v-model="queryParams.curLoad"-->
          <!--placeholder="请输入负荷"-->
          <!--clearable-->
          <!--size="small"-->
          <!--@keyup.enter.native="handleQuery"-->
        <!--/>-->
      <!--</el-form-item>-->
      <!--<el-form-item label="容量" prop="approvalLoad">-->
        <!--<el-input-->
          <!--v-model="queryParams.approvalLoad"-->
          <!--placeholder="请输入容量"-->
          <!--clearable-->
          <!--size="small"-->
          <!--@keyup.enter.native="handleQuery"-->
        <!--/>-->
      <!--</el-form-item>-->
      <!--<el-form-item label="安装位置" prop="installPosition">-->
        <!--<el-input-->
          <!--v-model="queryParams.installPosition"-->
          <!--placeholder="请输入安装位置"-->
          <!--clearable-->
          <!--size="small"-->
          <!--@keyup.enter.native="handleQuery"-->
        <!--/>-->
      <!--</el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['energy:nyTrans:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['energy:nyTrans:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['energy:nyTrans:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-upload"
          size="mini"
          @click="handleImport"
          v-hasPermi="['energy:nyTrans:edit']"
        >导入</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['energy:nyTrans:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="nyTransList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!--<el-table-column label="编号" align="center" prop="id" />-->
      <el-table-column label="编码" align="center" prop="num" />
      <el-table-column label="名称" align="center" prop="name" />
      <!--<el-table-column label="设备分类" show-overflow-tooltip align="center" prop="deviceType" />-->
      <el-table-column label="所属园区" align="center" prop="parkNum" />
      <el-table-column label="执行标准" align="center" prop="standard" />
      <el-table-column label="制造日期" align="center" prop="makeDate" width="80">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.makeDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="投入时间" align="center" prop="useDate" width="80">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.useDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="出厂编号" align="center" prop="factoryNum" />
      <el-table-column label="总重量(KG)" align="center" prop="weight" />
      <el-table-column label="厂家" align="center" prop="factory" />
      <el-table-column label="上一次年检时间" align="center" prop="preCheckTime" width="80">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.preCheckTime, '{y}-{m}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="下一次年检时间" align="center" prop="nextCheckTime" width="80">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.nextCheckTime, '{y}-{m}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="安装位置" align="center" prop="installPosition" />
      <el-table-column label="额定容量" align="center" prop="approvalLoad" />
      <el-table-column label="负荷" align="center" prop="curLoad" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="showFileDowFn(scope.row)"
            v-hasPermi="['energy:nyTrans:edit']"
          >下载</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['energy:nyTrans:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['energy:nyTrans:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <FileDownPop v-if="showFilePop" @close="filePopClose" :fileList="fileList"></FileDownPop>

    <!-- 抄表导入对话框 -->
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">
          将文件拖到此处，或
          <em>点击上传</em>
        </div>
        <div class="el-upload__tip" slot="tip">
          <!--<el-checkbox v-model="upload.updateSupport" />是否更新已经存在的数据-->
          <el-link type="info" style="font-size:12px" @click="importTemplate">下载模板</el-link>
        </div>
        <div class="el-upload__tip" style="color:red" slot="tip">提示：仅允许导入“xls”或“xlsx”格式文件！</div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加或修改变压器台账对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <!--<el-form-item label="编码" prop="num">-->
          <!--<el-input v-model="form.num" placeholder="请输入编码" />-->
        <!--</el-form-item>-->
        <!--<el-form-item label="编码" prop="num">-->
        <!--<el-input v-model="form.num" placeholder="请输入编码" disabled />-->
        <!--</el-form-item>-->
        <el-form-item label="设备分类" v-if="!form.id" prop="sysBmId">
          <treeselect v-model="form.sysBmId" :options="sysBmTypeOptions" :normalizer="bmNormalizer"
                       placeholder="设备分类"/>
        </el-form-item>
        <el-form-item label="名称" prop="name">
        <el-input v-model="form.name" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="园区" prop="parkNum">
          <el-select v-model="form.parkNum" placeholder="请选择园区">
            <el-option
              v-for="item in parkList"
              :key="item.id"
              :label="item.parkName"
              :value="item.parkNum"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="安装位置" prop="installPosition">
          <el-input v-model="form.installPosition" placeholder="请输入安装位置" />
        </el-form-item>
        <el-form-item label="执行标准" prop="standard">
          <el-input v-model="form.standard" placeholder="请输入执行标准" />
        </el-form-item>
        <el-form-item label="制造日期" prop="makeDate">
          <el-date-picker clearable size="small"
                          v-model="form.makeDate"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="选择制造日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="投入时间" prop="useDate">
          <el-date-picker clearable size="small"
                          v-model="form.useDate"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="选择制造日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="出厂编号" prop="factoryNum">
          <el-input v-model="form.factoryNum" placeholder="请输入出厂编号" />
        </el-form-item>
        <el-form-item label="总重量(KG)" prop="weight">
          <el-input v-model="form.weight" placeholder="请输入总重量" />
        </el-form-item>
        <el-form-item label="生产厂家" prop="factory">
          <el-input v-model="form.factory" placeholder="请输入生产厂家" />
        </el-form-item>
        <el-form-item label="上一次年检时间" prop="preCheckTime">
          <el-date-picker clearable size="small"
                          v-model="form.preCheckTime"
                          type="month"
                          value-format="yyyy-MM-dd"
                          placeholder="选择上一次年检时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="下一次年检时间" prop="nextCheckTime">
          <el-date-picker clearable size="small"
                          v-model="form.nextCheckTime"
                          type="month"
                          value-format="yyyy-MM-dd"
                          placeholder="选择下一次年检时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="额定容量" prop="approvalLoad">
          <el-input v-model="form.approvalLoad" placeholder="请输入容量" />
        </el-form-item>
        <!--<el-form-item label="负荷" prop="curLoad">-->
          <!--<el-input v-model="form.curLoad" type="number" placeholder="请输入负荷" />-->
        <!--</el-form-item>-->
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="附件地址">
          <FileUpload ref="fileupload" :fileListTem="form.fileIdList" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listNyTrans, getNyTrans, delNyTrans, addNyTrans, updateNyTrans } from "@/api/smartParks/energy/nyTrans";
import {listBuiPark} from "@/api/parkAssets/enterCommunity";
import FileUpload from '@/views/smartParks/parkAssets/components/FileUpload';
import { getToken } from "@/utils/auth";

export default {
  name: "NyTrans",
  components: {
    FileUpload,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 变压器台账表格数据
      nyTransList: [],
      // 园区编码字典
      parkList: [],
      fileList:[],
      showFilePop:false,
      sysBmTypeOptions:this.$store.state.permission.sysBmTree,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        num: null,
        curLoad: null,
        approvalLoad: null,
        installPosition: null,
        parkNum: null,
      },
      // 用户导入参数
      upload: {
        // 是否显示弹出层
        open: false,
        // 弹出层标题
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/energy/nyTrans/importData"
      },
      // 表单参数
      form: {fileIdList: [],},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "箱变名称不能为空", trigger: "blur" }
        ],
        sysBmId:[{ required: true, message: "设备分类不能为空", trigger: "blur" }],
        parkNum: [
          { required: true, message: "所属园区不能为空", trigger: "blur" }
        ],
        nextCheckTime: [
          { required: true, message: "下一次年检不能为空", trigger: "blur" }
        ],
        approvalLoad: [
          { required: true, message: "额定容量不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getParkList();
  },
  methods: {
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "抄表导入";
      this.upload.open = true;
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert(response.msg, "导入结果", { dangerouslyUseHTMLString: true });
      this.getList();
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    },
    /** 下载模板操作 */
    importTemplate() {
      this.download('energy/nyTrans/importTemplate',{
        ...this.queryParams
      }, `箱变台账.xlsx`)
    },
    filePopClose(){
      this.showFilePop=false
    },
    showFileDowFn(row){
      this.reset();
      const id = row.id || this.ids;
      getNyTrans(id).then((response) => {
        response.data.fileList.forEach((element) => {
          let url= "http://" + element.url
          this.fileList.push({
            name: element.name,
            id: element.id,
            url:url,
          })
        });
      });
      this.showFilePop=true
    },
    /** 查询变压器台账列表 */
    getList() {
      this.loading = true;
      listNyTrans(this.queryParams).then(response => {
        this.nyTransList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getParkList(){
      //查询园区列表，赋值给parkList;
      listBuiPark().then((response) =>{
        this.parkList = response.rows;
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        deviceType: null,
        num: null,
        name: null,
        curLoad: null,
        approvalLoad: null,
        installPosition: null,
        parkNum: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        standard: null,
        makeDate: null,
        factoryNum: null,
        weight: null,
        factory: null,
        preCheckTime: null,
        nextCheckTime: null,
        fileIdList: []
      };
      this.fileList=[];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加变压器台账";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getNyTrans(id).then(response => {
        this.form = response.data;
        this.form.fileIdList = [];
        this.form.fileList.forEach((element) => {
          let url= "http://" + element.url
          this.form.fileIdList.push({
            name: element.name,
            id: element.id,
            url,
          });
          this.fileList.push(url)
        });
        this.open = true;
        this.title = "修改变压器台账";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        console.log("valid",valid)
        console.log("sysBmId",this.form.sysBmId)
        if (valid) {
          let data = JSON.parse(JSON.stringify(this.form));
          data.fileIdList = this.$refs.fileupload.fileList.map(
            (item) => item.id
          );
          if (this.form.id != null) {
            updateNyTrans(data).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            //if(this.form.sysBmId == null){}
            addNyTrans(data).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除变压器台账编码为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delNyTrans(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('energy/nyTrans/export', {
        ...this.queryParams
      }, `energy_nyTrans.xlsx`)
    },

  }
};
</script>
<style lang='scss'>
  //  type='number'样式去除
  /*input::-webkit-outer-spin-button,*/
  /*input::-webkit-inner-spin-button {*/
  /*-webkit-appearance: none;*/
  /*}*/
  input[type="number"]{
  -moz-appearance: textfield;
}
</style>
